package com.cjg.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.locks.ReentrantLock;

public class demo02 {
   static AtomicStampedReference<Integer> asr = new AtomicStampedReference<>(10, 1);
    public static void main(String[] args) {

        new ReentrantLock();

        new Thread(()->{
            int stamp = asr.getStamp();
            System.out.println(stamp);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(asr.compareAndSet(10, 1, asr.getStamp(), asr.getStamp() + 1));
            System.out.println("a2=>"+asr.getStamp());
            System.out.println(asr.compareAndSet(1, 10, asr.getStamp(), asr.getStamp() + 1));
            System.out.println("a3=>"+asr.getStamp());

        },"a").start();



        new Thread(()->{
            int stamp = asr.getStamp();
            System.out.println(stamp);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(asr.compareAndSet(10, 16, stamp, stamp+1));
            System.out.println("b2=>"+asr.getStamp());

        },"b").start();
    }
}
